package com.gome.ss.webserver.server;

import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;

import com.gome.ss.common.AppConfiguer;
import com.gome.ss.common.Utils;

public class WebServer {
	private static Logger logger = Logger.getLogger(WebServer.class);
	
    Server server = null;
    WebAppContext context = null;
    
    public WebServer() {
    	server = new Server();
    	
        SelectChannelConnector connector = new SelectChannelConnector();  
        connector.setPort(AppConfiguer.getIntByKey("webserverPort"));  
        connector.setMaxIdleTime(30000);
        connector.setRequestHeaderSize(40960);
        server.setConnectors(new Connector[]{ connector });
        
    	context = new WebAppContext();
        context.setDescriptor(System.getProperty("user.dir") + "/src/main/webapp/WEB-INF/web.xml");
        context.setResourceBase("src/main/webapp");  
        context.setDisplayName("SSServer");
        context.setClassLoader(Thread.currentThread().getContextClassLoader());  
        context.setConfigurationDiscovered(true);  
        context.setParentLoaderPriority(true);  
        server.setHandler(context);
    }
    
    public void start() {
        try {
			server.start();
			server.join();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(Utils.getTrace(e));
		}
    }
    
}
